﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Workflow.Alerts.Interfaces.ProfileSettings;
using Sitecore.Data.Items;

namespace Workflow.Alerts.Sublayouts.ProfileSettings
{
    public partial class NewMaterial : BaseControl, IMaterialsDtlsView
    {
        #region Fields
        int id;
        #endregion

        #region INewMaterialView Members

        public int? Id
        {
            get
            {
                if (Request.QueryString["mId"] != null && Int32.TryParse(Request.QueryString["mId"], out id))
                {
                    return id;
                }
                return null;
            }
        }

        public string Name
        {
            get { return Server.HtmlEncode(txtName.Text); }
            set { txtName.Text = value; }
        }

        public float? Price
        {
            get
            {
                if (!String.IsNullOrEmpty(txtPrice.Text))
                {
                    return (float)txtPrice.Value;
                }
                return null;
            }
            set { txtPrice.Value = value.Value; }
        }

        public float? ConstructorPrice
        {
            get
            {
                if (!String.IsNullOrEmpty(txtConstructorPrice.Text))
                {
                    return (float)txtConstructorPrice.Value;
                }
                return null;
            }
            set { txtConstructorPrice.Value = value.Value; }
        }

        public IDictionary<int, string> Units
        {
            set
            {
                ddlUnit.DataSource = value;
                ddlUnit.DataValueField = "key";
                ddlUnit.DataTextField = "value";

                ddlUnit.DataBind();
            }
        }

        public int Unit
        {
            get
            {
                if (ddlUnit.SelectedIndex == -1) return 1;
                return ddlUnit.SelectedIndex + 1;
            }
            set { ddlUnit.Items.FindByValue(value.ToString()).Selected = true; }
        }

        public bool Disable
        {
            get { return chkDisable.Checked; }
            set { chkDisable.Checked = value; }
        }

        public event OnMaterialsDtlsViewLoad OnPageLoad;
        public event OnMaterialsDtlsViewUpdate OnUpdate;


        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            PresentationManager.RegisterView(typeof(MaterialsDtlsPresenter), this, new WebSessionProvider());

            if (!IsPostBack && OnPageLoad != null)
            {
                OnPageLoad();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            //if (Id.HasValue && OnPageLoad != null)//show confirmation window
            //{
            //    OnPageLoad();
            //}
            //else
            //{
            //    ClearForm();
            //}
            //bug 2031 'Cancel button returns to previous screen'
            var path = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item.Parent);
            Response.Redirect(path);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (OnUpdate != null)
            {
                if (OnUpdate())
                {

                    Item detailPageItem = Sitecore.Context.Item.Axes.GetAncestors()[Sitecore.Context.Item.Axes.Level - 1];
                    var path = Sitecore.Links.LinkManager.GetItemUrl(detailPageItem);
                    Response.Redirect(path);
                }
                else
                {
                    lblErrorName.Visible = true;
                    lblErrorName.Text = string.Format("Name of material '{0}' already exist, please use another one.", txtName.Text);
                }
            }

        }

        protected void btnSaveNew_Click(object sender, EventArgs e)
        {
            if (OnUpdate != null)
            {
                if (OnUpdate())
                {

                    var path = Sitecore.Links.LinkManager.GetItemUrl(Sitecore.Context.Item);
                    Response.Redirect(path);
                }
                else
                {
                    lblErrorName.Visible = true;
                    lblErrorName.Text = string.Format("Name of material '{0}' already exist, please use another one.", txtName.Text);
                }
            }

        }

        #endregion

        #region Private Methods

        private void ClearForm()
        {
            Name = String.Empty;
            Price = (float)0;
            Unit = 0;
            Disable = false;
        }

        #endregion
    }
}